package com.xrr.shardingdemo.sharding;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Range;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
public class DateShardingAlgorithm extends ShardingAlgorithmTool<Long> {

    /**
     * 获取 指定分表
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> preciseShardingValue) {
        return shardingTablesCheckAndCreatAndReturn(preciseShardingValue.getLogicTableName(), preciseShardingValue.getLogicTableName()+preciseShardingValue.getValue()%10);
    }

    /**
     * 获取 范围分表
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> rangeShardingValue) {
//        Range<Long> valueRange = rangeShardingValue.getValueRange();
//        Long lowerDate = valueRange.lowerEndpoint();
//        Long upperDate = valueRange.upperEndpoint();
//        List<String> tableNameList = new ArrayList<>();
//        for (DateTime dateTime : DateUtil.rangeToList(DateUtil.beginOfDay(lowerDate), DateUtil.endOfDay(upperDate), DateField.DAY_OF_YEAR)) {
//            String resultTableName = rangeShardingValue.getLogicTableName() + DateUtil.format(dateTime, "_yyyy_MM_dd");
//            if (shardingTablesExistsCheck(resultTableName)) {
//                tableNameList.add(resultTableName);
//            }
//        }
//        return tableNameList;
        return null;
    }
}
